import request from '@/utils/request'

// 模拟岗位数据
const mockPostData = [
  {
    id: '1',
    postName: '深圳北站执勤点',
    postCategory: '1',
    postType: '1',
    department: '1',
    locationType: 1,
    postLocation: '深圳市龙华区深圳北站',
    longitude: 114.085947,
    latitude: 22.547,
    validDate: '2024-01-01,2024-12-31',
    shifts: [
      {
        id: '1',
        shiftName: '早班',
        shiftTimePeriod: '08:00:00-16:00:00',
        isEnabled: 1
      },
      {
        id: '2',
        shiftName: '晚班',
        shiftTimePeriod: '16:00:00-24:00:00',
        isEnabled: 1
      }
    ]
  },
  {
    id: '2',
    postName: '福田口岸巡逻点',
    postCategory: '2',
    postType: '2',
    department: '1',
    locationType: 1,
    postLocation: '深圳市福田区福田口岸',
    longitude: 114.114634,
    latitude: 22.550751,
    validDate: '2024-01-01,2024-12-31',
    shifts: [
      {
        id: '3',
        shiftName: '白班',
        shiftTimePeriod: '06:00:00-18:00:00',
        isEnabled: 1
      }
    ]
  },
  {
    id: '3',
    postName: '罗湖口岸服务点',
    postCategory: '2',
    postType: '1',
    department: '1',
    locationType: 1,
    postLocation: '深圳市罗湖区罗湖口岸',
    longitude: 114.226478,
    latitude: 22.545431,
    validDate: '2024-01-01,2024-12-31',
    shifts: [
      {
        id: '4',
        shiftName: '全天班',
        shiftTimePeriod: '00:00:00-24:00:00',
        isEnabled: 1
      }
    ]
  }
];

// 查询岗位列表
export function listPost(query) {
  // 使用模拟数据
  /* 注释掉模拟数据代码
  const { pageNum = 1, pageSize = 10, postName, postCategory, postType } = query;
  
  let filteredData = [...mockPostData];
  
  // 根据查询条件过滤
  if (postName) {
    filteredData = filteredData.filter(item => item.postName.includes(postName));
  }
  if (postCategory) {
    filteredData = filteredData.filter(item => item.postCategory === postCategory);
  }
  if (postType) {
    filteredData = filteredData.filter(item => item.postType === postType);
  }
  
  // 分页
  const start = (pageNum - 1) * pageSize;
  const end = start + pageSize;
  const rows = filteredData.slice(start, end);
  
  return Promise.resolve({
    code: 200,
    rows: rows,
    total: filteredData.length
  });
  */
  
  // 实际接口调用
  return request({
    url: '/system/post/list',
    method: 'get',
    params: query
  })
}

// 查询岗位详细
export function getPost(postId) {
  // 使用模拟数据
  /* 注释掉模拟数据代码
  const post = mockPostData.find(item => item.id === postId);
  
  if (post) {
    return Promise.resolve({
      code: 200,
      data: post
    });
  } else {
    return Promise.reject(new Error('岗位不存在'));
  }
  */
  
  // 实际接口调用
  return request({
    url: '/system/post/' + postId,
    method: 'get'
  })
}

// 新增岗位
export function addPost(data) {
  // 使用模拟数据
  /* 注释掉模拟数据代码
  const newPost = {
    ...data,
    id: String(mockPostData.length + 1)
  };
  mockPostData.push(newPost);
  
  return Promise.resolve({
    code: 200,
    message: '新增成功'
  });
  */
  
  // 实际接口调用
  return request({
    url: '/system/post',
    method: 'post',
    data: data
  })
}

// 修改岗位
export function updatePost(data) {
  // 使用模拟数据
  /* 注释掉模拟数据代码
  const index = mockPostData.findIndex(item => item.id === data.id);
  if (index !== -1) {
    mockPostData[index] = { ...mockPostData[index], ...data };
    return Promise.resolve({
      code: 200,
      message: '修改成功'
    });
  } else {
    return Promise.reject(new Error('岗位不存在'));
  }
  */
  
  // 实际接口调用
  return request({
    url: '/system/post',
    method: 'put',
    data: data
  })
}

// 删除岗位
export function delPost(postId) {
  // 使用模拟数据
  /* 注释掉模拟数据代码
  const index = mockPostData.findIndex(item => item.id === postId);
  if (index !== -1) {
    mockPostData.splice(index, 1);
    return Promise.resolve({
      code: 200,
      message: '删除成功'
    });
  } else {
    return Promise.reject(new Error('岗位不存在'));
  }
  */
  
  // 实际接口调用
  return request({
    url: '/system/post/' + postId,
    method: 'delete'
  })
}

// 保存岗位和班次信息
export function savePostWithShifts(data) {
  // 使用模拟数据
  /* 注释掉模拟数据代码
  if (data.id) {
    // 更新
    return updatePost(data);
  } else {
    // 新增
    return addPost(data);
  }
  */
  
  // 实际接口调用
  return request({
    url: '/system/post/saveWithShifts',
    method: 'post',
    data: data
  })
}

// 一次性新增岗位和班次信息
export function addPostWithShifts(data) {
  // 使用模拟数据
  /* 注释掉模拟数据代码
  const newPost = {
    ...data,
    id: String(mockPostData.length + 1)
  };
  mockPostData.push(newPost);
  
  return Promise.resolve({
    code: 200,
    data: newPost.id,
    message: '新增成功'
  });
  */
  
  // 实际接口调用
  return request({
    url: '/system/post/addWithShifts',
    method: 'post',
    data: data
  })
}
